// 定义user模型
const mongoose = require("mongoose")
var runValidatorsPlugin = function(schema, options) {
  schema.pre('updateOne', function(next) {
    this.options.runValidators = true
    next()
  })
	schema.pre('updateMany', function(next) {
    this.options.runValidators = true
    next()
  })
}
const userSchema = new mongoose.Schema({
	openId: {
		type: String,
		unique: true,	
	},
	name: {
		type: String,
		default: '昵称',
		required:[true,'昵称不能为空'],
		minlength:[2,'昵称长度不能少于2'],
		maxlength:[10,'昵称长度不能多于10'],
		trim:true
	},
	avatar: {
		type: String,
		default: '',
	},
	age: {
		type: Number,
		default: 18,
	},
	sex: {
		type: String,
		default: '未知',
		enum: ['未知','男', '女'],
		required: function() {
			return this.age > 20
		}
	},
	address: String,
	createTime: {
		type: Date,
		default: new Date()
	}, 
})
userSchema.plugin(runValidatorsPlugin);
const userModel = mongoose.model("users", userSchema)
module.exports = userModel